﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using PloobsEngine.Material;
using PloobsEngine.Cameras;

namespace PloobsEngine.SceneControl
{
    public class comparer : IComparer<IObject>
    {   
        #region IComparer<IObject> Members

        public int Compare(IObject x, IObject y)
        {
            if (x.CameraDistance > y.CameraDistance)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        #endregion
    }

    public class ForwardPass : IForwardPass
    {

        comparer c = new comparer();
        public void Draw(GameTime gt, IWorld world,IRenderHelper render)        
        {
            IEnumerable<IObject> list = world.Culler.GetNotCulledObjectsList(world,world.ActiveCamera,MaterialType.FORWARD );            
            if(sortByCameraDistance)
                list.OrderByDescending((a) => a,c);

            //foreach (IObject item in world.Objects.Where((a, b) => ( a.Material.MaterialType & Engine.Material.MaterialType.FORWARD) == Engine.Material.MaterialType.FORWARD))
            foreach (IObject item in list)
	        {
                    item.PosDrawn(gt, world.CameraManager.ActiveCamera, world.Lights, render);
	        } 

        }


        #region IForwardPass Members

        bool sortByCameraDistance = false;
        public bool EnableSortByCameraDistance
        {
            get
            {
                return sortByCameraDistance;
            }
            set
            {
                sortByCameraDistance = value;
            }
        }

        #endregion
    }
}
